package com.einmeer.kssc_administrator.service.impl;

import com.einmeer.kssc_administrator.entity.Administrators;
import com.einmeer.kssc_administrator.mapper.AdministratorsMapper;
import com.einmeer.kssc_administrator.scconst.AdministratorsConst;
import com.einmeer.kssc_administrator.service.AdministratorsService;
import com.einmeer.kssc_administrator.vo.ResultVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 芊嵛
 * @date 2024/1/31
 */
@Service
@Transactional
public class AdministratorsServiceImpl implements AdministratorsService {
    @Resource
    private AdministratorsMapper administratorsMapper;

    /**
     * 登录
     *
     * @param administratorsUsername
     * @param administratorsPassword
     * @return
     */
    @Override
    public ResultVo login(String administratorsUsername, String administratorsPassword) {
        // 判断用户是否存在
        Integer id1 = administratorsMapper.selectAdministratorsIdByUN(administratorsUsername);
        // 判断用户密码是否正确
        Integer id2 = administratorsMapper.selectAdministratorsIdByUNAndPW(administratorsUsername, administratorsPassword);
        // 账号存在
        if (id1 != null) {
            // 成功登录
            if (id2 !=null ) {
                return new ResultVo(AdministratorsConst.ADMINISTRATORSOK, id2, AdministratorsConst.LOGINOK);
            }else {
                // 密码错误
                return new ResultVo(AdministratorsConst.ADMINISTRATORSNO, AdministratorsConst.LOGINNO2);
            }
        } else {
            // 账号不存在
            return new ResultVo(AdministratorsConst.ADMINISTRATORSNO,  AdministratorsConst.LOGINNO1);
        }
    }

    /**
     * 返回用户信息
     * @param administratorsId
     * @return
     */
    @Override
    public ResultVo administratorsInfo(Integer administratorsId) {
        Administrators administrators = administratorsMapper.selectAdministratorsById(administratorsId);
        if (administrators!=null){
            return new ResultVo(AdministratorsConst.ADMINISTRATORSOK,administrators,AdministratorsConst.SELECTOK);
        }
        return new ResultVo(AdministratorsConst.ADMINISTRATORSNO,AdministratorsConst.SELECTNO);
    }

    /**
     * 修改密码
     * @param administratorsId
     * @param administratorsOldPassWord
     * @param administratorsNewPassWord
     * @return
     */
    @Override
    public ResultVo changeAdministratorPassword(Integer administratorsId, String administratorsOldPassWord, String administratorsNewPassWord) {
        if(administratorsOldPassWord.equals(administratorsNewPassWord)){
            return new ResultVo(AdministratorsConst.ADMINISTRATORSNO,"旧密码不能与新密码相同");
        }
        int n =0;
        n = administratorsMapper.updateAdministratorsByIdAndPassword(administratorsId, administratorsOldPassWord, administratorsNewPassWord);
        if (n==1){
            return  new ResultVo(AdministratorsConst.ADMINISTRATORSOK,AdministratorsConst.UPDATEOK);
        }
        return new ResultVo(AdministratorsConst.ADMINISTRATORSNO,AdministratorsConst.UPDATENO);
    }
}
